package cn.hrfax.stf.sdk.integration.abs;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.hrfax.stf.sdk.model.RemoteResponse;
import cn.hrfax.stf.sdk.template.AbstractCallRemoteAction;
import cn.hrfax.stf.sdk.template.ActionTemplate;
import cn.hrfax.stf.sdk.util.ValidateUtil;

/**
 * 调用基础服务
 * @author yongSen.wang
 * @date 2020/6/15 16:24
 */
@Service
public abstract class AbstractRemote {

    @Resource
    private ActionTemplate actionTemplate;

    /**
     * 通用的业务请求模板
     * @param remoteServiceName
     * @param urlPath
     * @param orderNo
     * @param req
     * @param busiCode
     * @param <P>
     * @return
     * @throws RuntimeException
     */
    protected <P> RemoteResponse callRemote(final String remoteServiceName, final String urlPath,
                                            String orderNo, P req,
                                            final String busiCode) throws RuntimeException {
        return this.actionTemplate
            .execute(new AbstractCallRemoteAction<RemoteResponse, P>(remoteServiceName, req) {
                @Override
                public void checkParams(P req) throws RuntimeException {
                    ValidateUtil.validate(req);
                }

                @Override
                public RemoteResponse doAction(P req) throws RuntimeException {
                    return getRemoteResult(urlPath, orderNo, req, busiCode);
                }
            });
    }

    /**
     * 获取远程响应数据
     * @param urlPath
     * @param orderNo
     * @param req
     * @param busiCode
     * @param <P>
     * @return
     * @throws RuntimeException
     */
    protected abstract <P> RemoteResponse getRemoteResult(final String urlPath, String orderNo,
                                                          P req,
                                                          String busiCode) throws RuntimeException;
}
